import axios from "axios";
import { ElMessage } from "element-plus";
import { GET_TOKEN } from "./token";

// 配置基础路径 和 超时时间
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
});

// 添加请求拦截器
request.interceptors.request.use((config) => {
  config.headers.satoken = GET_TOKEN();
  return config;
});

// 添加响应拦截器
request.interceptors.response.use(
  (response) => {
    const code = response.data.code
    if (code === 200) {
      return response.data;
    } else {
      const message = response.data.msg
      ElMessage.error(message);
      return Promise.reject();
    }
  },
  (error) => {
    ElMessage.error("服务器异常");
    return Promise.reject(error);
  }
);

export default request;
